#include <pybind11/pybind11.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "demo.h"

namespace py = pybind11;

int open_wrapper(const char* pathname, int flags, mode_t mode = 0) {
    if (flags & O_CREAT) {
        return open(pathname, flags, mode);
    } else {
        return open(pathname, flags);
    }
}

PYBIND11_MODULE(example, m) {
    m.doc() = "pybind11 封装的文件操作模块";

    init_demo_module(m);

    m.def("open", &open_wrapper, 
        py::arg("pathname"), 
        py::arg("flags"), 
        py::arg("mode") = 0,
        "调用系统的open()函数\n"
        "flags参数可以是以下值的组合:\n"
        "O_RDONLY: 只读模式\n"
        "O_WRONLY: 只写模式\n"
        "O_RDWR: 读写模式\n"
        "O_CREAT: 如果文件不存在则创建\n"
        "O_TRUNC: 截断文件\n"
        "O_APPEND: 追加模式");

    m.attr("O_RDONLY") = O_RDONLY;
    m.attr("O_WRONLY") = O_WRONLY;
    m.attr("O_RDWR") = O_RDWR;
    m.attr("O_CREAT") = O_CREAT;
    m.attr("O_TRUNC") = O_TRUNC;
    m.attr("O_APPEND") = O_APPEND;

    m.attr("S_IRUSR") = S_IRUSR;
    m.attr("S_IWUSR") = S_IWUSR;
    m.attr("S_IXUSR") = S_IXUSR;
    m.attr("S_IRGRP") = S_IRGRP;
    m.attr("S_IWGRP") = S_IWGRP;
    m.attr("S_IXGRP") = S_IXGRP; 
}

